package com.study.jyg.rxjava2study.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.study.jyg.rxjava2study.R;
import com.study.jyg.rxjava2study.utils.ToastUtils;
import com.study.jyg.rxjava2study.utils.dialog.DialogUtils;
import com.study.jyg.rxjava2study.utils.dialog.dialogFragment.BaseDialog;

import butterknife.ButterKnife;

public abstract class BasicFragment extends Fragment implements IBaseView {

    protected Context mContext;
    public View mRootView;
    private BaseDialog dialog;
    private View emptyView;

    private boolean isVisibleToUser;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.mContext = context;

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        if (mRootView == null) {
            if (getLayoutRes() != 0) {
                mRootView = LayoutInflater.from(mContext)
                        .inflate(getLayoutRes(), container, false);
                ButterKnife.bind(this, mRootView);
                initView();
            } else {
                throw new RuntimeException("layoutResID==-1 have u create your layout?");
            }
        }
        return mRootView;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        if (isVisibleToUser) {
            Log.e("fragment", getClass().getSimpleName());
        }
    }

    /**
     * 获取contentView 资源id
     */
    public abstract int getLayoutRes();

    public abstract void initView();


    @Override
    public void showLoading() {
        if (null == dialog)
            dialog = DialogUtils.showLoading(getChildFragmentManager());
    }

    @Override
    public void hideLoading() {
        if (dialog != null)
            dialog.dismiss();
        dialog = null;
    }

    @Override
    public void toastShort(String msg) {
        ToastUtils.show(msg);
    }

    @Override
    public void toastLong(String msg) {
        ToastUtils.showLong(msg);
    }

    /**
     * activity跳转（无参数）
     *
     * @param className
     */

    public void startActivity(Class<?> className) {
        Intent intent = new Intent(mContext, className);
        startActivity(intent);
    }

    /**
     * activity跳转（有参数）
     *
     * @param className
     */
    public void startActivity(Class<?> className, Bundle bundle) {
        Intent intent = new Intent(mContext, className);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     *
     */
    public void doSomething(String... todo){

    }
    public void doSomething(int... todo){

    }

    /**
     * 对外接口
     */
    public void refresh(){

    }


    /**
     * 对外接口
     */
    public void loadMore(){

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        hideLoading();
    }
}
